/*
 * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sina.weibo.sdk.openapi;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboParameters;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.utils.LogUtil;

/**
 * 该类提供了获取用户信息接口，帮助开发者方便获取用户信息。
 * @see <a href="http://http://open.weibo.com/wiki/2/users/show">用户信息</a>
 * 
 * @author 郑成武
 * @since 2013-12-13
 */
public class UserInfoAPI extends AbsOpenAPI {
    private static final String TAG = UserInfoAPI.class.getName();

    /** 用户信息地址（URL） */
    private static final String USERINFO_URL = "https://api.weibo.com/2/users/show.json";
    
    /**
     * 构造函数。
     * 
     * @param oauth2AccessToken Token 实例
     */
    public UserInfoAPI(Oauth2AccessToken oauth2AccessToken) {
        super(oauth2AccessToken);
    }

    /**
     * 异步获取用户信息。
     * 
     * @param listener 请求后的回调接口
     */
    public void getUserInfo(RequestListener listener) {
        if (mAccessToken != null && mAccessToken.isSessionValid() && listener != null) {
            WeiboParameters params = new WeiboParameters();
            params.add("uid", mAccessToken.getUid());
            params.add("access_token", mAccessToken.getToken());
            request(USERINFO_URL, params, HTTPMETHOD_GET, listener);
        } else {
            LogUtil.e(TAG, "UserInfo args error!");
        }
    }
    
    /**
     * 同步获取用户信息。
     * 
     * @param listener 请求后的回调接口
     */
    public String getUserInfoSync() {
    	String resp = "";
        if (mAccessToken != null && mAccessToken.isSessionValid()) {
            WeiboParameters params = new WeiboParameters();
            params.add("uid", mAccessToken.getUid());
            params.add("access_token", mAccessToken.getToken());
            resp = requestSync(USERINFO_URL, params, HTTPMETHOD_GET);
        } else {
            LogUtil.e(TAG, "UserInfo args error!");
        }
        return resp;
    }
}
